<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin\CodeRecord;
use App\Models\Member\PayConfig;
use App\Common\WechatHandler;
use DB;
class CodeRecordController extends Controller
{
    public function getList(Request $request)
    {
        $pageSize = $request['pageSize'];
        $storeId = $request['storeId'];
        $userId = $request['userId'];
        $phone = $request['phone'];

        $codeRecordModel = new CodeRecord;
        $data = $codeRecordModel->getList($pageSize, $storeId, $phone, $userId);

        return $data;
    }
    // 生成微信菜单
    public function createProcess(Request $request){
        $storeId = $request["storeId"];
        $res=DB::table("wechat_menu")->where(['status'=>1,'level'=>0,'pid'=>0,'store_id'=>$storeId])->limit(3)->get();
      
        $data = '{"button":[';
        $y = 1;
        // $storeId = 21;
        $wxConfig = PayConfig::where('store_id', $storeId)->first()->toArray();
        foreach ($res as $key=>$value) {
            //主菜单
            $data.='{"name":"' . $value->menu_name . '",';
            $second = DB::table("wechat_menu")->where(['pid' => $value->id,'status'=>1,'level'=>1,'store_id'=>$storeId])->limit(5)->get();
 
            //子菜单
            if (isset($second[0]))
            {
                $data.='"sub_button":[';
            }else{
                if ($value->menu_url)
                {
                    $data.='"type":"view","url":"' . $value->menu_url . '"';
                }else{
                    $data.='"type":"click","key":"key1"';
                }
            }
            $i = 1;
            foreach ($second as $k=>$v)
            {
                if ($i == count($second))
                {
                    //小程序配置小程序跳转设置
                    if($v->id==33){
                        $data.='{"type":"miniprogram","name":"' . $v->menu_name . '","url":"' . $v->menu_url . '","appid":"'.$wxConfig['app_id'].'","pagepath":"pages/show/show"}]';
                    }else{
                        if ($v->menu_url)
                        {
                            $data.='{"type":"view","name":"' . $v->menu_name . '","url":"' . $v->menu_url . '"}]';
                        }else{
                            $data.='{"type":"click","name":"' . $v->menu_name . '","key":"key2"}]';
                        }
                    }
                }else{
                    if($v->id==33){
                        $data.='{"type":"miniprogram","name":"' . $v->menu_name . '","url":"' . $v->menu_url . '","appid":"'.$wxConfig['app_id'].'","pagepath":"pages/show/show"},';
                    }else{
                        if ($v->menu_url)
                        {
                            $data.='{"type":"view","name":"' . $v->menu_name . '","url":"' . $v->menu_url . '"},';
                        }else{
                            $data.='{"type":"click","name":"' . $v->menu_name . '","key":"key3"},';
                        }
                    }
                }
                $i++;
            }

            if ($y == count($res))
            {
                $data.='}';
            }else{
                $data.='},';
            }
            $y++;
        }
        $data.=']}';
        $access_token=WechatHandler::getAccessToken($storeId, $wxConfig);
        // return $access_token;
        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        echo $this->createMenu($data,$url);
    }


    public function createMenu($data,$url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
            if (curl_errno($ch))
            {
                return curl_error($ch);
            }
        curl_close($ch);
        return $tmpInfo;
    }
}
